﻿namespace EyeOpen.Dependency.Unity
{
    using System;
    using System.Collections.Generic;
    using EyeOpen.Dependency;
    using Microsoft.Practices.Unity;
    
    public class UnityDependencyContainer : IDependencyContainer
    {
        private UnityContainer container;

        private UnityContainer Container
        {
            get
            {
                if (container == null)
                {
                    container = new UnityContainer();

                    return container;
                }

                return container;
            }
        }

        public T Resolve<T>()
        {   
            return 
                Container
                .Resolve<T>();
        }

        public T Resolve<T>(string name)
        {
            return
                Container              
                .Resolve<T>(name);
        }

        public object Resolve(Type t)
        {
            return Container.Resolve(t);
        }

        public object Resolve(Type t, string name)
        {
            return Container.Resolve(t, name);
        }

        public IEnumerable<T> ResolveAll<T>()
        {
            return Container.ResolveAll<T>();
        }

        public IEnumerable<object> ResolveAll(Type t)
        {
            return Container.ResolveAll(t);
        }

        public void RegisterInstance<TInterface>(TInterface instance)
        {
            Container
                .RegisterInstance(instance);
        }

        public void RegisterInstance<TInterface>(string name, TInterface instance)
        {
            Container
                .RegisterInstance(name, instance, new ContainerControlledLifetimeManager());
        }

        public void RegisterInstance<TInterface>(TInterface instance, Lifetime lifetime)
        {
            Container
                .RegisterInstance(instance, new ContainerControlledLifetimeManager());
        }

        public void Teardown(object o)
        {
            Container.Teardown(o);
        }
    }
}